package com.javasea.web.httpclient.retrofit;

import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.exception.ReadResponseBodyException;
import com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;

/**
 * 错误解码器。ErrorDecoder.
 * 当请求发生异常或者收到无效响应结果的时候，将HTTP相关信息解码到异常中，无效响应由业务自己判断
 *
 * When an exception occurs in the request or an invalid response result is received, the HTTP related information is decoded into the exception,
 * and the invalid response is determined by the business itself.
 *
 * @author 陈添明
 */
public class MyErrorDecoder implements ErrorDecoder {
    @Override
    public RuntimeException invalidRespDecode(Request request, Response response) {
        ResponseBody body = response.body();
        System.out.println(body);
        String s = "";
        try {
            s = RetrofitUtils.readResponseBody(response);
        } catch (ReadResponseBodyException e) {
            e.printStackTrace();
        }
        System.out.println(">>>>"+s);
        return new RuntimeException(s);
    }
}